//
//  LGProductDetailViewController.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/28.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGProductDetailViewController.h"
#import "UIViewController+NavRegister.h"
#import "LGDetailCollectionViewCell.h"
#import "LGProductDetailHeader.h"
#import "LGProductDetailReusableView.h"
#import "LGProductDetailParamCell.h"
#import "LGProductDetailBottomBar.h"
#import "LGProductShareAlert.h"
#import "LGPopView.h"
#import "LGPayWayViewController.h"

@interface LGProductDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (strong,nonatomic)UICollectionView *collectionView;
@property (strong,nonatomic)UIView *header;
@property (strong,nonatomic)LGProductDetailBottomBar *bar;
@end

@implementation LGProductDetailViewController

- (instancetype)init
{
    if (self = [super init])
    {
        self.hiddenNavigationBar = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LGDetailCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGProductDetailHeader" bundle:nil] forCellWithReuseIdentifier:@"LGProductDetailHeader"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGProductDetailReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"LGProductDetailReusableView"];
     [self.collectionView registerNib:[UINib nibWithNibName:@"LGProductDetailParamCell" bundle:nil] forCellWithReuseIdentifier:@"LGProductDetailParamCell"];
    
    
    [self.view addSubview:self.bar];
    [self.bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.size.mas_equalTo(self.bar.size);
    }];
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(-STATUSBAR_HEIGHT, 0, self.bar.height, 0));
    }];
    
    [self.view addSubview:self.header];
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.header.size);
        make.top.left.offset(0);
    }];

    
}

- (LGProductDetailBottomBar *)bar
{
    if(_bar == nil)
    {
        LGProductDetailBottomBar *bar = [LGProductDetailBottomBar createFromXib];
        bar.size = CGSizeMake(SCREEN_WIDTH, 50);
        _bar = bar;
        WeakObj(self);
        //客服按钮
        [bar.kefuBtn bk_addEventHandler:^(id sender) {
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        //购物车按钮
        [bar.gowucheBtn bk_addEventHandler:^(id sender) {
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        //加入购物车
        [bar.jiaruBtn bk_addEventHandler:^(id sender) {
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        //立即下单购买
        [bar.lijiBtn bk_addEventHandler:^(id sender) {
            
            LGPayWayViewController *vc = [LGPayWayViewController createFromXib];
            [selfWeak.navigationController pushViewController:vc animated:YES];
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _bar;
}
- (UIView *)header
{
    if (_header == nil)
    {
        _header =  [UIView new];
        _header.frame = CGRectMake(0,0, SCREEN_WIDTH, NAVIGATOR_HEIGHT);
        
        CGFloat bottom = (NAVIGATOR_HEIGHT - STATUSBAR_HEIGHT - 32) / 2.0;
        
        UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame = CGRectMake(0, 0, 32, 32);
        [leftBtn setImage:LGImageFor(@"packup") forState:UIControlStateNormal];
        WeakObj(self);
        [leftBtn bk_addEventHandler:^(id sender) {
            [selfWeak popOrDismiss];
        } forControlEvents:UIControlEventTouchUpInside];
        [_header addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.bottom.offset(-bottom);
        }];
        
        
        UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(0, 0, 32, 32);
        [rightBtn setImage:LGImageFor(@"fenxiang") forState:UIControlStateNormal];
        [rightBtn bk_addEventHandler:^(id sender) {
            [selfWeak showShareAlert];
        } forControlEvents:UIControlEventTouchUpInside];
        [_header addSubview:rightBtn];
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-15);
            make.bottom.offset(-bottom);
        }];
        
        
    }
    return _header;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
//        //上下边距
//        layout.minimumLineSpacing = 10;
//        //左右边距
//        layout.minimumInteritemSpacing = 5;
        //每组内容的缩进
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0);
        
        /*
         以垂直方向滑动为例子
         cellCount = (CollectionViewContentWidth-sectionInset.left-sectionInset.right+minimumInteritemSpacing)/(itemSize.width+minimumInteritemSpacing)
         */
        
        //缓存高度
        //    layout.estimatedItemSize = CGSizeMake(30, 60);
        
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = UIColorFromRGB(0xEFF1F1);
        _collectionView = collectionView;
    }
    return _collectionView;
}
//组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}
//每行具体多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0)
    {
        return 1;
    }
    else if (section == 1)
    {
        return 5;
    }
    else if (section == 2)
    {
        return 4;
    }
    else if (section == 3)
    {
        return 1;
    }
    else if (section == 4)
    {
        return 4;
    }
    return 1;
}
//上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    else if (section == 1)
    {
        return 0;
    }
    return 7;
}

//左右间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    else if (section == 1)
    {
        return 0;
    }
    else if (section == 4)
    {
        return 5;
    }
    return 7;
}

//cell size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0)
    {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH + 110);
    }
    else if (indexPath.section == 1)
    {
        return CGSizeMake(SCREEN_WIDTH, 50);
    }
    CGSize size = CGSizeMake(floor((SCREEN_WIDTH - 5) / 2.0), 237);
    return size;
}
//Header size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1 || section == 2 || section == 4)
    {
        return CGSizeMake(SCREEN_WIDTH, 50);
    }
    return CGSizeZero;
}

//头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 4)
    {
       LGProductDetailReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LGProductDetailReusableView" forIndexPath:indexPath];
        NSString *str;
        if (indexPath.section == 1)
        {
            str = @"商品参数";
        }
        else if(indexPath.section == 2)
        {
            str = @"服务保障";
        }
        else if(indexPath.section == 4)
        {
            str = @"猜你喜欢";
        }
        view.titleLab.text = str;
        return view;
    }
    return nil;
}

//具体cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0)
    {
        LGProductDetailHeader *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LGProductDetailHeader" forIndexPath:indexPath];
        //    cell.lab.text = _dataArr[indexPath.row];
        
//        cell.imageView.image = [UIImage imageNamed:@"1"];
        cell.banner.localizationImageNamesGroup = @[@"1",@"2",@"3",@"4"];
        
        return cell;
    }
    else if (indexPath.section == 1)
    {
        
        LGProductDetailParamCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LGProductDetailParamCell" forIndexPath:indexPath];
        cell.titleLab.text = @"品牌";
        cell.detailLab.text = @"百搭翡翠（patek philippe）";
        return cell;
    }
    else
    {
        LGDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LGDetailCollectionViewCell" forIndexPath:indexPath];
        //    cell.lab.text = _dataArr[indexPath.row];
        
        cell.imageView.image = [UIImage imageNamed:@"1"];
        
        return cell;
    }
    
   
}

- (void)showShareAlert
{
    LGPopView *pop = [LGPopView new];
    pop.postion = LGPopupPostionCenter;
    LGProductShareAlert *alert = [LGProductShareAlert createFromXib];
    alert.size = CGSizeMake(SCREEN_WIDTH - 24 * 2, 186);
    
    [pop addInView:self.view withContentView:alert];
    [pop showWithDidShowBlock:^(LGPopView *view) {
        
    }];
    
    alert.close = ^(LGProductShareAlert *alert) {
        [pop hiddenWithDidHiddenBlock:nil];
    };
    
    alert.copy = ^(LGProductShareAlert *alert) {
        [pop hiddenWithDidHiddenBlock:nil];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
